package com.example.drop

import android.icu.util.Calendar
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.TimeUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.drop.adapter.DataAdapter
import com.example.drop.adapter.TaskAdapter
import com.example.drop.databinding.ActivityDropBinding
import com.example.drop.entity.DataEntity
import com.example.drop.vm.DropViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path = "/drop/DropActivity")
class DropActivity : BaseMvvmActivity<ActivityDropBinding>() {
    override fun getLayoutId(): Int=R.layout.activity_drop
    val dataAdapter=DataAdapter()
    var list = mutableListOf<DataEntity>()
    val dropViewModel:DropViewModel by viewModels()
    val taskAdapter=TaskAdapter()
    override fun initView() {
        super.initView()
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        //跳转点位管理
        mDataBinding.keep.setOnClickListener {
            ARouter.getInstance().build("/drop/PointActivity").navigation()
        }
        mDataBinding.tvData.setText(SimpleDateFormat("yyyy-MM").format(System.currentTimeMillis()))
        var calendar =Calendar.getInstance()
        var week=calendar.get(Calendar.DAY_OF_WEEK)
        for(i in -(week-1) .. (week-1)+6){
            calendar=Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_MONTH,i)
            var day=calendar.get(Calendar.DAY_OF_MONTH)
            var weeek1= TimeUtils.getChineseWeek(calendar.time).substring(1,2)
            list.add(DataEntity(weeek1,day.toString(),if (i==0) true else false))
        }
        mDataBinding.rvData.adapter=dataAdapter
        mDataBinding.rvData.layoutManager= GridLayoutManager(this,9)
        dataAdapter.addData(list)
        dropViewModel.getTask()
        mDataBinding.rvTask.adapter=taskAdapter
        mDataBinding.rvTask.layoutManager=LinearLayoutManager(this)
        dropViewModel.mSuccessLiveData.observe(this){
            taskAdapter.data.clear()
            taskAdapter.addData(it.data.sortedByDescending { it.id })
            taskAdapter.notifyDataSetChanged()
        }
        dropViewModel.mErrorLiveData.observe(this){
            TimeUtils.getChineseWeek(it)
        }
        taskAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build( "/drop/DropXiangActivity")
                .withSerializable("task",taskAdapter.data[position])
                .navigation()
        }

    }

}